package com.spirng.leetcode.day07;

public class Test450 {
    public TreeNode deleteNode(TreeNode root, int key) {
        if(root==null){
            return null;
        }
        if(root.val==key){
            if(root.left!=null&&root.right!=null){
                TreeNode cur=root.right;
                while (cur.left!=null){
                    cur=cur.left;
                }
                cur.left=root.left;
                return root.right;
            }
            if(root.left==null&&root.right==null){
                return null;
            }
            return root.left!=null?root.left:root.right;
        }
        if(root.val>key){
            root.left=deleteNode(root.left,key);
        }
        if(root.val<key){
            root.right=deleteNode(root.right,key);
        }
        return root;
    }
}
